package com.thoughtworks;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class App {

  public static void main(String[] args) {
    // 以下是执行交易的交易员和发生的一系列交易,请为以下八个查询方法提供实现。
    Trader raoul = new Trader("Raoul", "Cambridge");
    Trader mario = new Trader("Mario", "Milan");
    Trader alan = new Trader("Alan", "Cambridge");
    Trader brian = new Trader("Brian", "Cambridge");
    List<Transaction> transactions = Arrays.asList(new Transaction(brian, 2011, 300),
        new Transaction(raoul, 2012, 1000),
        new Transaction(raoul, 2011, 400),
        new Transaction(mario, 2012, 710), new Transaction(mario, 2012, 700),
        new Transaction(alan, 2012, 950)
    );

    // 1.找出2011年的所有交易并按交易额排序(从低到高)
    System.out.println(get2011Transactions(transactions));

    // 2.交易员都在哪些不同的􏱜城市工作过
    System.out.println(getTradersCity(transactions));
//
//    // 3.查找所有来自于剑桥的交易员，并按姓名排序
    System.out.println(getCambridgeTraders(transactions));
//
//    // 4.返回所有交易员的姓名字符串，按字母顺序排序
    System.out.println(getTradersName(transactions));
//
//    // 5.有没有交易员是在米兰工作的
    System.out.println(hasMilanTrader(transactions));
//
//    // 6.返回交易员是剑桥的所有交易的交易额
    System.out.println(getCambridgeTransactionsValue(transactions));
//
//    // 7.所有交易中，最高的交易额是多少
    System.out.println(getMaxTransactionValue(transactions));
//
//    // 8.返回交易额最小的交易
    System.out.println(getMinTransaction(transactions));
  }

  public static List<Transaction> get2011Transactions(List<Transaction> transactions) {
    Stream<Transaction> stream = transactions.stream();
    Stream<Transaction> stream2 = stream.filter((Transaction tra)->{return tra.getYear()==2011;});
    List<Transaction> result = stream2.collect(Collectors.toList());
    return result;
  }

  public static List<String> getTradersCity(List<Transaction> transactions) {
    Stream<Transaction> transactionStream = transactions.stream();
    Stream<String> traderCity = transactionStream.map((Transaction tra)->{return tra.getTrader().getCity();}).distinct();
    List<String> result = traderCity.collect(Collectors.toList());
    return result;
  }

  public static List<Trader> getCambridgeTraders(List<Transaction> transactions) {
    Stream<Trader> transactionStream = transactions.stream().map((Transaction::getTrader));
    Stream<Trader> traderStream = transactionStream.filter((Trader trad)-> trad.getCity().equals("Cambridge")).distinct().sorted(Comparator.comparing(Trader::getName));
    List<Trader> result = traderStream.collect(Collectors.toList());
    return result;
  }

  public static List<String> getTradersName(List<Transaction> transactions) {
    List<String> result = transactions.stream().map((Transaction tra)->{return tra.getTrader().getName();}).distinct().sorted().collect(Collectors.toList());
    return result;
  }

  public static boolean hasMilanTrader(List<Transaction> transactions) {
    long numMilan = transactions.stream().filter((Transaction tra)-> tra.getTrader().getCity().equals("Milan")).count();
    if(numMilan>=1){
      return true;
    }else {
      return false;
    }
  }

  public static List<Integer> getCambridgeTransactionsValue(List<Transaction> transactions) {
    List<Integer> result = transactions.stream().filter((Transaction tra)->tra.getTrader().getCity().equals("Cambridge")).map((Transaction tra)->tra.getValue()).collect(Collectors.toList());
    return result;
  }

  public static int getMaxTransactionValue(List<Transaction> transactions) {
    int maxTransactionValue = transactions.stream().map((Transaction tra)->tra.getValue()).sorted(Comparator.reverseOrder()).collect(Collectors.toList()).get(0);
    return maxTransactionValue;
  }

  public static Transaction getMinTransaction(List<Transaction> transactions) {
    List<Integer> values = new ArrayList<>();
    for(Transaction tra: transactions){
      values.add(tra.getValue());
    }
    Integer minValue = values.stream().sorted().collect(Collectors.toList()).get(0);
    Transaction result = transactions.stream().filter((Transaction tra)->{return tra.compareValue(minValue);}).collect(Collectors.toList()).get(0);
    return result;
  }
}
